package com.seecoder.BlueWhale.controller;

import com.seecoder.BlueWhale.service.OrderService;
import com.seecoder.BlueWhale.util.SecurityUtil;
import com.seecoder.BlueWhale.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/orders")
@Slf4j
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    SecurityUtil securityUtil;

    @PostMapping("/create")
    public ResultVO<OrderVO> create(@RequestBody OrderVO orderVO){
        log.info("User[{}] begins create order.", securityUtil.getUserId());
        ResultVO<OrderVO> ans = ResultVO.buildSuccess(orderService.create(orderVO));
        log.info("User[{}] ends create order.", securityUtil.getUserId());
        return ans;
    }

    @PostMapping("/pay")
    public ResultVO<Boolean> pay(@RequestBody PayParams params, HttpServletResponse httpServletResponse){
        log.warn("User[{}] begins pay order.", securityUtil.getUserId());
        if(orderService.pay(params, httpServletResponse)) {
            log.warn("User[{}] pays order successfully.", securityUtil.getUserId());
            return null;
        }
        log.warn("User[{}] pays order failed.", securityUtil.getUserId());
        return ResultVO.buildSuccess(false);
    }

    @PostMapping("/dispatch")
    public ResultVO<Boolean> dispatch(@RequestParam Integer orderID){
        log.info("User[{}] begins dispatch order.", securityUtil.getUserId());
        ResultVO<Boolean> ans = ResultVO.buildSuccess(orderService.dispatch(orderID));
        log.info("User[{}] ends dispatch order.", securityUtil.getUserId());
        return ans;
    }

    @PostMapping("/receive")
    public ResultVO<Boolean> receive(@RequestParam Integer orderID){
       log.info("User[{}] begins receive order.", securityUtil.getUserId());
        ResultVO<Boolean> ans = ResultVO.buildSuccess(orderService.receive(orderID));
        log.info("User[{}] ends receive order.", securityUtil.getUserId());
        return ans;
    }
    @PostMapping ("/info")
    public ResultVO<PageVO<OrderVO>> info(@RequestParam("page") int page, @RequestParam("pageSize") int size){
        log.info("Begin to get order info.");
        ResultVO<PageVO<OrderVO>> ans = ResultVO.buildSuccess(orderService.info(page, size));
        log.info("End to get order info.");
        return ans;
    }

    @PostMapping("/comment")
    public ResultVO<CommentVO> comment(@RequestBody CommentVO commentVO){
        log.info("User[{}] begins comment.", securityUtil.getUserId());
        ResultVO<CommentVO> ans = ResultVO.buildSuccess(orderService.comment(commentVO));
        log.info("User[{}] ends comment.", securityUtil.getUserId());
        return ans;
    }

    @PostMapping("/info/single")
    public ResultVO<OrderVO> infoSingle(@RequestParam ("id") int id){
        log.info("Begin to get order info[Single].");
        ResultVO<OrderVO> ans = ResultVO.buildSuccess(orderService.infoSingle(id));
        log.info("End to get order info[Single].");
        return ans;
    }

    @PostMapping("/get_comment")
    public ResultVO<CommentVO> getComment(@RequestParam ("id") int id){
        log.info("Begin to get comment.");
        ResultVO<CommentVO> ans = ResultVO.buildSuccess(orderService.getComment(id));
        log.info("End to get comment.");
        return ans;
    }

    @GetMapping("/excel")
    public ResultVO<String> getExcel() {
        log.info("Begin to get excel.");
        ResultVO<String> ans = ResultVO.buildSuccess(orderService.getExcel());
        log.info("End to get excel.");
        return ans;
    }
}
